package cn.qingxuntech.console.controller

import cn.qingxuntech.console.api.ServerApiClient
import cn.qingxuntech.console.entity.SspSpace
import cn.qingxuntech.console.model.R
import cn.qingxuntech.console.model.request.FuncLogRequest
import cn.qingxuntech.console.model.request.SspSpaceRequest
import cn.qingxuntech.console.service.SspSpaceService
import com.alibaba.cloud.nacos.NacosDiscoveryProperties
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties
import com.mybatisflex.core.paginate.Page
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cloud.client.discovery.DiscoveryClient
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/qx/console/v1/server")
class SspServerController {

    @Autowired
    lateinit var serverApiClient: ServerApiClient

    @Autowired
    lateinit var discoveryClient: DiscoveryClient

    @GetMapping("list")
    fun list(): R<List<String>> {

        val serviceInstances = discoveryClient.getInstances("mucheen-server")
        return R.success(serviceInstances.map { "http://${it.host}:${it.port}" })
    }

    @PostMapping("pushLogRecord")
    fun pushLogRecord(@RequestBody funcLogRequest: FuncLogRequest): R<Boolean> {

        serverApiClient.pushLogRecord(funcLogRequest.url, funcLogRequest)
        return R.success(true)
    }

}